{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "11ab22d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5f13bcf2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     PassengerId  Survived  Pclass     Sex     fare  SibSp  Parch\n",
      "0              1         0       3    male   7.2500      1      0\n",
      "1              2         1       1  female  71.2833      1      0\n",
      "2              3         1       3  female   7.9250      0      0\n",
      "3              4         1       1  female  53.1000      1      0\n",
      "4              5         0       3    male   8.0500      0      0\n",
      "..           ...       ...     ...     ...      ...    ...    ...\n",
      "886          887         0       2    male  13.0000      0      0\n",
      "887          888         1       1  female  30.0000      0      0\n",
      "888          889         0       3  female  23.4500      1      2\n",
      "889          890         1       1    male  30.0000      0      0\n",
      "890          891         0       3    male   7.7500      0      0\n",
      "\n",
      "[891 rows x 7 columns]\n",
      "342\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQkAAAEYCAYAAABP4gNaAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAcgElEQVR4nO3deZzd46HH8c+TmcmenMQSRMQhFKGINfZ9PaVbtLSo7Sq13Etv+V29OFXLj97WUtVbai1Kb1GtI2ipomjUrg1SchRJiBiTZDKZzPLcP56TGuPML7Od8zznnO/79ZrXxMxZvnPM+c7z/LbHWGsREenJEN8BRCRsKgkRSaSSEJFEKgkRSaSSEJFEKonAGWPqjDHrruI20/r4mBOMMbcaYzbq5e2PNMaM6ctz9PJxRw72Y8rgU0kExBhTb4w51xhzvDHmYmPMd4HTgaeMMRcn3PVGY8yOvXj84caYrwC/Aq4D5ibctusbeDG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KPcgbhl5j/jO4vQCJwL/G8+zmj70QCpJAZROsrVA8fiLuU/yXOcWrQc+BlwYT7O6Opjg0QlUQKFy/h/C/gvYE3PcWrBCuA64OJ8nJnnO0y1UUmUUDrKjQbOKHyM9xynGrXhzrG5MB9n3vYdplqpJMogHeVGAcfjyiLtN01VaAKuB36sszVLTyVRRukoVwd8Efh3YFfPcSrRa8BVuMV5teu5TFQSnqSj3Da40cXhwGqe44SsHXgQ+DHwUD7O6Be2zFQSnqWj3FDgEOAbwEFAvd9EwZiFOwjqjnyced93mFqmkghIOspNAI7AHZi1C1DnN1HZzQF+CdyajzNzfIcRRyURqHSUG48bWRwCHAiM8xqoNFqBx3AnXd1fWGFNAqOSqACFg7R2BfYufN4RGOk1VP90An8HnsAVw8PaABk+lUQFKpTG1rjC2AXYDlgfMB5jFbMI+AvwFPA0MCsfZxb7jSR9pZKoEoVjMTbDnb4+tfB5E2BdSntFLQu8jbsM3GtdP+fjzLslfF4pE5VEDUhHuXHAxC4f6wBjcFOWEd0+D8Ed5tza5XMr7ryIRbgV2RcC84F5wPx8nGkr308j5aaSEJFEuuiMiCRSSYhIIpWEiCRSSYhIIpWEiCRSSYhIIpWEiCRSSYhIIpWEiCRSSYhIIpWEiCRSSYhIIpWEiCRSSYhIIpWEiCRSSYhIIpWEiCRSSYhIIpWEiCRSSYhIIpWEiCRSSYhIIpWEiCRSSYhIIpWEiCRSSYhIIpWEiCRSSYhIIpWEiCRSSYhIIpWEiCT6fzQ/Pzr2aPpiAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 1\n",
    "titanic = pd.read_csv('./titanic.csv')\n",
    "print(titanic)\n",
    "\n",
    "# 2\n",
    "# 幸存\n",
    "titanic_survived = titanic[titanic['Survived'] == 1]\n",
    "# 生还者人数\n",
    "print(len(titanic_survived))\n",
    "\n",
    "# 3 饼图\n",
    "plt.rcParams['font.family'] = 'KaiTi'\n",
    "male = len(titanic[titanic['Sex'] == 'male'])\n",
    "female = len(titanic[titanic['Sex'] == 'female'])\n",
    "popularity = [male, female]\n",
    "plt.pie(popularity, labels=['男', '女'], autopct='%1.1f%%', counterclock=False, startangle=90)\n",
    "plt.title('男女乘客比例')\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "938e08a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAAESCAYAAAAVLtXjAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAUpElEQVR4nO3df7jedX3f8efLAJomgIGcBlMXs2i6zSoBmosmDOiBErfMalvWVa8qbkObzguv7hrXNmml6+x0ZUydrR24rNE5nbjYTkalYNRJzVZQk6LItVapbYCBzGBiIEwE9b0/vl+Ws3PukxPg3Pfh3J/n47q4+H7f3+/9vT+f5OR1f87n++NOVSFJasuzFroBkqTRM/wlqUGGvyQ1yPCXpAYZ/pLUIMNfTUrykiTHPYXXrU1y6lG2P2fK8gVJzjjKvsuSvHjqa5Oc9GTbJD0Vhr+akyTAtcDaafUZ/x6SvCDJ1UnelWQ38I+Ak49y+HcmeWW//DPAjx9l328Dvzdl/YeBD83dA+npM/zVolcA7wS+kWSiD/Z3A7dP37Gq7gZuAfbSBfWtwOokS6fvm+TZwI8AN/W/AawB7k9y+qBGVNX3gU9NKf0csDfJR5L82NPonzQnw19NSfJy4DzgbOCXgAeB44GPAjum7Xtikn8OvBA4C7gIuB94I/APBhz+TcA7q+rxfvmqqvoo8AtJJmdp0v4kJyT5S8Ah4N3AacCfPuVOSsfA8Fdr7gEOAyfSBezzgHuBpcBEkq1PjOqr6mHgOcBf9Nv3043sDwG/OfWgSVYDPw8c6Of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0y/CWpQYa/JDXo/wIk+Fr0ylIqeAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 4 船票直方图\n",
    "fare = titanic.sort_values('fare')['fare']\n",
    "\n",
    "plt.hist(fare, bins=10) \n",
    "plt.title(\"船票价格\")\n",
    "plt.xlabel(\"价格\")\n",
    "plt.ylabel(\"数量\")\n",
    " \n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d1adfc93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Sex</th>\n",
       "      <th>fare</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>familysize</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>886</th>\n",
       "      <td>887</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>13.0000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>887</th>\n",
       "      <td>888</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>30.0000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>888</th>\n",
       "      <td>889</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>23.4500</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>889</th>\n",
       "      <td>890</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>30.0000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>890</th>\n",
       "      <td>891</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>7.7500</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>891 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     PassengerId  Survived  Pclass  Sex     fare  SibSp  Parch  familysize\n",
       "0              1         0       3    1   7.2500      1      0           1\n",
       "1              2         1       1    0  71.2833      1      0           1\n",
       "2              3         1       3    0   7.9250      0      0           0\n",
       "3              4         1       1    0  53.1000      1      0           1\n",
       "4              5         0       3    1   8.0500      0      0           0\n",
       "..           ...       ...     ...  ...      ...    ...    ...         ...\n",
       "886          887         0       2    1  13.0000      0      0           0\n",
       "887          888         1       1    0  30.0000      0      0           0\n",
       "888          889         0       3    0  23.4500      1      2           3\n",
       "889          890         1       1    1  30.0000      0      0           0\n",
       "890          891         0       3    1   7.7500      0      0           0\n",
       "\n",
       "[891 rows x 8 columns]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5\n",
    "titanic['familysize'] = titanic['SibSp'] + titanic['Parch']\n",
    "\n",
    "# 6\n",
    "titanic['Sex'] = titanic['Sex'].map({'male': 1, 'female': 0})\n",
    "\n",
    "print(titanic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c336c463",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 0 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 1\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0\n",
      " 0 0 1 0 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 1 0 1 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0\n",
      " 0 1 1 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0\n",
      " 1 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "data = titanic[['Pclass', 'Sex', 'familysize']]\n",
    "label = titanic['Survived']\n",
    "\n",
    "# 7\n",
    "# 划分数据集\n",
    "x_train, x_test,  y_train, y_test = train_test_split(data, label, test_size = 0.2, random_state = 7)\n",
    "\n",
    "# 8\n",
    "# 构建KNN模型\n",
    "kn = KNeighborsClassifier(n_neighbors=5)\n",
    "kn.fit(x_train, y_train)\n",
    "\n",
    "# 9\n",
    "# 预测结果\n",
    "pre = kn.predict(x_test)\n",
    "print(pre)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "2cc35f03",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/30\n",
      "22/22 [==============================] - 3s 40ms/step - loss: 0.6967 - accuracy: 0.6193 - val_loss: 0.8265 - val_accuracy: 0.3750\n",
      "Epoch 2/30\n",
      "22/22 [==============================] - 0s 10ms/step - loss: 0.6228 - accuracy: 0.6193 - val_loss: 0.7922 - val_accuracy: 0.3750\n",
      "Epoch 3/30\n",
      "22/22 [==============================] - 0s 10ms/step - loss: 0.5893 - accuracy: 0.6193 - val_loss: 0.7941 - val_accuracy: 0.3750\n",
      "Epoch 4/30\n",
      "22/22 [==============================] - 0s 10ms/step - loss: 0.5610 - accuracy: 0.6222 - val_loss: 0.7804 - val_accuracy: 0.3750\n",
      "Epoch 5/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.5381 - accuracy: 0.6463 - val_loss: 0.8094 - val_accuracy: 0.3750\n",
      "Epoch 6/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.5229 - accuracy: 0.6974 - val_loss: 0.8101 - val_accuracy: 0.5000\n",
      "Epoch 7/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.5123 - accuracy: 0.7827 - val_loss: 0.7942 - val_accuracy: 0.6250\n",
      "Epoch 8/30\n",
      "22/22 [==============================] - 0s 10ms/step - loss: 0.5069 - accuracy: 0.8011 - val_loss: 0.8771 - val_accuracy: 0.6250\n",
      "Epoch 9/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4981 - accuracy: 0.7969 - val_loss: 0.7852 - val_accuracy: 0.6250\n",
      "Epoch 10/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4981 - accuracy: 0.7884 - val_loss: 0.8321 - val_accuracy: 0.5000\n",
      "Epoch 11/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4870 - accuracy: 0.8111 - val_loss: 0.8293 - val_accuracy: 0.6250\n",
      "Epoch 12/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4855 - accuracy: 0.8097 - val_loss: 0.8193 - val_accuracy: 0.6250\n",
      "Epoch 13/30\n",
      "22/22 [==============================] - 0s 10ms/step - loss: 0.4795 - accuracy: 0.8168 - val_loss: 0.8015 - val_accuracy: 0.6250\n",
      "Epoch 14/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4766 - accuracy: 0.8153 - val_loss: 0.8370 - val_accuracy: 0.6250\n",
      "Epoch 15/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4742 - accuracy: 0.8182 - val_loss: 0.8010 - val_accuracy: 0.6250\n",
      "Epoch 16/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4665 - accuracy: 0.8168 - val_loss: 0.8297 - val_accuracy: 0.5000\n",
      "Epoch 17/30\n",
      "22/22 [==============================] - 0s 10ms/step - loss: 0.4684 - accuracy: 0.8125 - val_loss: 0.7884 - val_accuracy: 0.6250\n",
      "Epoch 18/30\n",
      "22/22 [==============================] - 0s 10ms/step - loss: 0.4664 - accuracy: 0.8168 - val_loss: 0.7903 - val_accuracy: 0.6250\n",
      "Epoch 19/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4632 - accuracy: 0.8210 - val_loss: 0.7974 - val_accuracy: 0.6250\n",
      "Epoch 20/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4578 - accuracy: 0.8196 - val_loss: 0.8075 - val_accuracy: 0.6250\n",
      "Epoch 21/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4559 - accuracy: 0.8210 - val_loss: 0.7858 - val_accuracy: 0.6250\n",
      "Epoch 22/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4555 - accuracy: 0.8224 - val_loss: 0.7981 - val_accuracy: 0.6250\n",
      "Epoch 23/30\n",
      "22/22 [==============================] - 0s 10ms/step - loss: 0.4529 - accuracy: 0.8139 - val_loss: 0.7846 - val_accuracy: 0.6250\n",
      "Epoch 24/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4523 - accuracy: 0.8224 - val_loss: 0.7743 - val_accuracy: 0.6250\n",
      "Epoch 25/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4500 - accuracy: 0.8253 - val_loss: 0.7810 - val_accuracy: 0.6250\n",
      "Epoch 26/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4503 - accuracy: 0.8054 - val_loss: 0.7541 - val_accuracy: 0.6250\n",
      "Epoch 27/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4480 - accuracy: 0.8210 - val_loss: 0.8148 - val_accuracy: 0.7500\n",
      "Epoch 28/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4512 - accuracy: 0.8182 - val_loss: 0.7580 - val_accuracy: 0.6250\n",
      "Epoch 29/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4514 - accuracy: 0.7997 - val_loss: 0.7395 - val_accuracy: 0.6250\n",
      "Epoch 30/30\n",
      "22/22 [==============================] - 0s 9ms/step - loss: 0.4473 - accuracy: 0.8239 - val_loss: 0.7998 - val_accuracy: 0.6250\n",
      "6/6 [==============================] - 0s 5ms/step\n",
      "[0 0 0 0 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 1 1 0 0 0 0\n",
      " 0 0 1 0 1 0 1 0 0 1 1 0 0 1 1 0 1 0 1 1 0 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0\n",
      " 0 1 1 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 1 0 0 1 0 1 0 1 0 0 0 0 0\n",
      " 1 0 0 0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.python.keras import activations\n",
    "from tensorflow.keras import regularizers\n",
    "\n",
    "# 10\n",
    "clf = tf.keras.Sequential(\n",
    "    [\n",
    "        tf.keras.layers.Dense(\n",
    "            64,\n",
    "            activation=\"relu\",\n",
    "            input_shape=(x_train.shape[1],),\n",
    "            kernel_regularizer=regularizers.l2(0.001),\n",
    "        ),\n",
    "        tf.keras.layers.Dense(\n",
    "            32, activation=\"relu\", kernel_regularizer=regularizers.l2(0.001)\n",
    "        ),\n",
    "        tf.keras.layers.Dense(16, activation=\"relu\"),\n",
    "        tf.keras.layers.Dense(1, name=\"prediction\"),\n",
    "    ]\n",
    ")\n",
    "\n",
    "clf.compile(\n",
    "        optimizer=tf.keras.optimizers.Adam(),\n",
    "        loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n",
    "        metrics=[\"accuracy\"],\n",
    "    )\n",
    "\n",
    "clf.fit(\n",
    "        x=x_train,\n",
    "        y=y_train,\n",
    "        epochs=30,\n",
    "        validation_split=0.01,\n",
    "        verbose=\"auto\",\n",
    "    )\n",
    "\n",
    "# 11\n",
    "pre1 = clf.predict(x_test)\n",
    "pre1 = (tf.sigmoid(pre1).numpy().flatten() > 0.5).astype(int)\n",
    "\n",
    "print(pre1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "68f32304",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.75      0.92      0.83       110\n",
      "           1       0.80      0.52      0.63        69\n",
      "\n",
      "    accuracy                           0.77       179\n",
      "   macro avg       0.78      0.72      0.73       179\n",
      "weighted avg       0.77      0.77      0.75       179\n",
      "\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.76      0.86      0.81       110\n",
      "           1       0.72      0.57      0.63        69\n",
      "\n",
      "    accuracy                           0.75       179\n",
      "   macro avg       0.74      0.71      0.72       179\n",
      "weighted avg       0.75      0.75      0.74       179\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 12\n",
    "from sklearn.metrics import classification_report\n",
    "\n",
    "# knn算法准确率，召回率\n",
    "print(classification_report(y_test, pre))\n",
    "\n",
    "# 神经网络准确率，召回率\n",
    "print(classification_report(y_test, pre1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e438098d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
